﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Filters_View : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      LoadFields();
    }
  }
  bool HasId()
  {
    int i;
    return int.TryParse(Request.Params["id"], out i);
  }
  int Id()
  {
    if (!HasId()) return -1;
    return int.Parse(Request.Params["id"]);
  }
  protected void bnSave_Click(object sender, EventArgs e)
  {
    Dictionary<string, object> values = new Dictionary<string, object>();
    foreach (KeyValuePair<WebControl, string> field in FieldMappings())
    {
      if (field.Key is TextBox)
        values[field.Value] = Util.ToSqlStr(((TextBox)field.Key).Text);
      if (field.Key is DropDownList)
        values[field.Value] = Util.ToSqlStr(((DropDownList)field.Key).SelectedValue);
    }
    int newId = Conn.UpdateOrInsert("filters", Id(), values);
    Response.Redirect("Filters-View.aspx?id=" + newId.ToString());
  }

  Dictionary<WebControl, string> FieldMappings()
  {
    Dictionary<WebControl, string> fieldMappings = new Dictionary<WebControl, string>();
    fieldMappings[txtFilter] = "filter";
    fieldMappings[txtWhereClause] = "where_clause";
    return fieldMappings;
  }

  void LoadFields()
  {
    if (HasId())
    {
      var dict = Conn.GetRowAsDict("select * from filters where id=@id", Id());

      litTitle.Text = "Filter: " + dict["filter"];
      foreach (KeyValuePair<WebControl, string> field in FieldMappings())
      {
        if (field.Key is TextBox)
          ((TextBox)field.Key).Text = Util.ToText(dict[field.Value]);
        if (field.Key is DropDownList)
        {
          ((DropDownList)field.Key).DataBind();
          ((DropDownList)field.Key).SelectedValue = Util.ToText(dict[field.Value]);
        }

      }
    }
    else
    {
      litTitle.Text = "New Filter";
    }
  }


  protected void bnDelete_Click(object sender, EventArgs e)
  {
    string deleteConfirm = "Confirm deletion";
    if (bnDelete.Text == deleteConfirm)
    {
      Conn.ExecuteNonQueryWithId("delete from filters where id=@id", Id());
      Response.Redirect("Filters.aspx");
    }
    else
    {
      bnDelete.Text = deleteConfirm;
    }
  }

  protected void bnExport_Click(object sender, EventArgs e)
  {
    // Export a list of members according to the current query
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=Export.xls");
    Response.Write(PeopleExport.DoExport(txtWhereClause.Text));
    Response.End();
  }
}
